#!/usr/bin/env python
# -*- coding : utf-8 -*-
#

import sys, os, os.path, time

import httplib


def httpget( host, path, repeats = 1 ) :
    bytes = 0
    count = 0

    hc = httplib.HTTPConnection( host )
    for i in xrange( repeats ) :
        hc.request( 'GET', path )
        response = hc.getresponse()
        if response.status != 200 :
            print response.status, response.reason
            return count,bytes

        data = response.read()
        count += 1
        bytes += len(data)
        ## for
    hc.close()

    return  count, bytes
    #### httpget()


host = '192.168.6.11:8360'
path = '/dulala.txt'

# try once
time_start = time.time()
count, bytes = httpget( host, path )
time_last = time.time() - time_start
tps = count / time_last 
page_size = bytes
print 'get %d times last %.2f seconds, tps=%.2f, page-size:%d' % (1, time_last, tps, page_size)

# do not keep live 
time_start = time.time()
t_times = 0
t_bytes = 0
for n in range( 1000 ) :
    count, bytes = httpget( host, path )
    t_times += count
    t_bytes += bytes
time_last = time.time() - time_start
tps = t_times / time_last 
page_size = t_bytes / t_times
print 'get %d times last %.2f seconds, tps=%.2f, page-size:%d' % (t_times, time_last, tps, page_size)


#  keep live 
time_start = time.time()
t_times = 0
t_bytes = 0
for n in range( 1 ) :
    count, bytes = httpget( host, path, 1000 )
    t_times += count
    t_bytes += bytes
time_last = time.time() - time_start
tps = t_times / time_last 
page_size = t_bytes / t_times
print 'get %d times last %.2f seconds, tps=%.2f, page-size:%d' % (t_times, time_last, tps, page_size)


